<?php

	class Taxonomy extends AppModel {
		var $name = 'Taxonomy';
		var $useTable = 'taxonomies';
		var $primaryKey = 'id';
		var $displayField = 'name';
		
		//var $actsAs = array('Tree');
		public $actsAs = array(
			'Ordered' => array(
				'field' => 'weight',
				'foreign_key' => false,
			)
		);
		
		var $belongsTo = array(
			'Term',
			'Shop'
		);
		
		var $hasAndBelongsToMany = array(
			'Product' => array(
				'className' => 'Product',
				'joinTable' => 'products_taxonomies',
				'foreignKey' => 'taxonomy_id',
				'associationForeignKey' => 'product_id'
			)
		);
		
		var $validate = array();
		
		function __construct() {
			parent::__construct();
			$this->validate = array(
				'Taxonomy' => array(
					'name' => array(
						'rule' => '/.+/',
						'message' => __('Debes indicarnos el nombre de la categoría.', true)
					)
				)
			);
		}
		
		function beforeSave() {
			// revisar la nueva categoría o tags de la tienda si exíste.
			if (!empty($this->data['Taxonomy']['shop_id'])) {
				$taxonomy_exists = $this->hasAny(
					array(
						'Taxonomy.shop_id' => $this->data['Taxonomy']['shop_id'],
						'Taxonomy.deleted' => 0,
						'Taxonomy.parent_id' => $this->data['Taxonomy']['parent_id'],
						'Taxonomy.name' => $this->data['Taxonomy']['name'],
						'Taxonomy.taxonomy' => $this->data['Taxonomy']['taxonomy']
					)
				);
				
				if ($taxonomy_exists) {
					return false;
				}
				else {
					$data = $this->Term->find(
						'first',
						array(
							'conditions' => array(
								'Term.deleted' => 0,
								'Term.name' => $this->data['Taxonomy']['name']
							),
							'fields' => array(
								'Term.id'
							),
							'recursive' => -1
						)
					);
					
					if (!empty($data)) {
						$this->data['Taxonomy']['term_id'] = $data['Term']['id'];
					}
					else {
						$this->data['Term']['name'] = $this->data['Taxonomy']['name'];
						$this->data['Taxonomy']['slug'] = $this->data['Term']['slug'] = strtolower(Inflector::slug($this->data['Taxonomy']['name'], '-'));
						$this->Term->save($this->data);
						$this->data['Taxonomy']['term_id'] = $this->Term->id;
					}
					
					return true;
				}
			}
			
			return true;
		}
		
		function categories($conditions = array()) {
			$conditions = array_merge(
				array(
					'Taxonomy.shop_id' => Configure::read('Shop.id'),
					'Taxonomy.taxonomy' => 'category'
				),
				$conditions
			);
			
			return $this->find(
				'threaded',
				array(
					'conditions' => $conditions,
					'recursive' => -1
				)
			);
		}
		
		function tags($conditions = array()) {
			$conditions = array_merge(
				array(
					'Taxonomy.shop_id' => Configure::read('Shop.id'),
					'Taxonomy.deleted' => 0,
					'Taxonomy.taxonomy' => 'tags'
				),
				$conditions
			);
			
			return $this->find(
				'list',
				array(
					'conditions' => $conditions,
					'recursive' => -1
				)
			);
		}
	}
